package org.glow.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.glow.changgou.goods.dao.BrandMapper;
import org.glow.changgou.goods.pojo.Brand;
import org.glow.changgou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-20 13:02:44
 * @Description
 * @Version 1.0
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandService brandService;
    @Resource
    private BrandMapper brandMapper;
    @Override
    public List<Brand> findAll() {
      return brandService.list();
    }

    @Override
    public Brand findById(Integer id) {
        return brandService.getById(id);
    }

    @Override
    @Transactional
    public void addNewBrand(Brand brand) {
        brandService.save(brand);
    }

    @Override
    public void updateBrandById(Brand brand,Integer id) {
        LambdaQueryWrapper<Brand>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Brand::getId,id);
        brandService.update(brand,queryWrapper);
    }

    @Override
    public void delete(Integer id) {
        brandService.removeById(id);
    }

    @Override
    public List<Brand> findList(Brand brand) {
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper = Condition(queryWrapper,brand);
//        if(!StringUtils.isEmpty(brand.getLetter()))
//        {
//            queryWrapper.eq(Brand::getId,brand.getId());
//        }
//        if(!StringUtils.isEmpty(brand.getSeq().toString()))
//        {
//            queryWrapper.eq(Brand::getSeq,brand.getSeq());
//        }
        return  brandService.list(queryWrapper);

    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        Page<Brand> pages = new Page<>(page,size);
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        return brandService.page(pages);
    }

    @Override
    public Page<Brand> findPage(Brand brand, int page, int size) {
        Page<Brand> pages = new Page<>(page,size);
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = Condition(queryWrapper,brand);
        return brandService.page(pages,queryWrapper);
    }

    @Override
    public List<Brand> findByCategory(Integer categoryid) {
        return brandMapper.findByCategory(categoryid);
    }

    public LambdaQueryWrapper<Brand> Condition(LambdaQueryWrapper<Brand> queryWrapper,Brand brand)
    {
        if(!StringUtils.isEmpty(brand.getName()))
        {
            queryWrapper.like(Brand::getName,brand.getName());
        }
        if(!StringUtils.isEmpty(brand.getImage()))
        {
            queryWrapper.like(Brand::getImage,brand.getImage());
        }
        if(!StringUtils.isEmpty(brand.getLetter()))
        {
            queryWrapper.like(Brand::getLetter,brand.getLetter());
        }
        return  queryWrapper;
    }
}
